package controller

import (
	"gin-epg/internal/app/client"
	"gin-epg/internal/app/common/rsp"
	"gin-epg/internal/app/service"
	"gin-epg/internal/app/store"
	"github.com/gin-gonic/gin"
	"io"
	"log"
	"runtime/debug"
	"time"
)

// ReadSeekCloser 适配器结构体，将 io.ReadSeeker 转换为 io.ReadCloser
type ReadSeekCloser struct {
	io.ReadSeeker
}

// Close 实现 io.Closer 接口
func (rsc *ReadSeekCloser) Close() error {
	return nil // 如果需要关闭底层资源，可以在这里实现
}

// 超级直播 http://127.0.0.1:8082/epginfo?channel=cctv1&date=20231206
// Diyp http://127.0.0.1:8082/diyp?ch=cctv1&date=2023-12-06
// xmltv 当天 http://127.0.0.1:8082/xmltv/e.xml 7天 http://127.0.0.1:8082/xmltv/e7.xml
func Epginfo(c *gin.Context) {
	ch := c.DefaultQuery("channel", "CCTV1")
	date := c.DefaultQuery("date", time.Now().Format("20060102"))
	todoList, stdch, err := service.Diyp(ch, date)
	if err != nil {
		rsp.Error(c, err.Error())
	} else {
		rsp.SuccessEpginfo(c, "请求成功", todoList, stdch.Name, stdch.Logo, date)
	}
}

func Diyp(c *gin.Context) {
	ch := c.DefaultQuery("ch", "CCTV1")
	date := c.DefaultQuery("date", time.Now().Format("2006-01-02"))
	todoList, stdch, err := service.Diyp(ch, date)
	if err != nil {
		rsp.Error(c, err.Error())
	} else {
		rsp.SuccessDiyp(c, "请求成功", todoList, stdch.Name, stdch.Logo, date)
	}
}

func Xmltv(c *gin.Context) {
	filename, ok := c.Params.Get("filename")
	if !ok {
		rsp.Error(c, "无效的id")
		return
	}

	storeService := store.GlobalStoreService
	fileStream, err := storeService.GetFileStream(filename)
	if err != nil {
		rsp.Error(c, err.Error())
		return
	}

	// 使用适配器将 io.ReadSeeker 转换为 io.ReadCloser
	readSeekCloser := &ReadSeekCloser{ReadSeeker: fileStream}

	// 使用封装的文件流响应处理方法
	rsp.FileResponse(c, filename, readSeekCloser, "application/octet-stream")
}

func Cron(c *gin.Context) {
	name := c.DefaultQuery("name", "")
	switch name {
	case "112114":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.Handle112114Cron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "51zmt":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.Handle51zmtCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "deny":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleDenyCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "erw":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleErwCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "zsdc":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleZsdcCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "cntv":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
					log.Printf("Stack trace: %s", debug.Stack())
				}
			}()

			if err := client.HandleCntvCron(0, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "hntv":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
					log.Printf("Stack trace: %s", debug.Stack())
				}
			}()

			if err := client.HandleHntvCron(0, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "sc":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
					log.Printf("Stack trace: %s", debug.Stack())
				}
			}()

			if err := client.HandleSc96655Cron(0, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "bfgd":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
					log.Printf("Stack trace: %s", debug.Stack())
				}
			}()

			if err := client.HandleBfgdCron(0, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "sdyx":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
					log.Printf("Stack trace: %s", debug.Stack())
				}
			}()

			if err := client.HandleSdyxCron(0, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "ady":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
					log.Printf("Stack trace: %s", debug.Stack())
				}
			}()

			if err := client.HandleAdyCron(0, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "kkn":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
					log.Printf("Stack trace: %s", debug.Stack())
				}
			}()

			if err := client.HandleKknCron(0, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "sports8":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleSports8Cron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "51livetv":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleWuyilivetvCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "ifeng":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
					log.Printf("Stack trace: %s", debug.Stack())
				}
			}()

			if err := client.HandleIfengCron(0, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "ystenfujian":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleYstenFujianCron(0, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "ystenjiangsu":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleYstenJiangsuCron(0, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "ystenliaoning":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleYstenLiaoningCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "ystenhunan":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleYstenHunanCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "hndx":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleHunanTelecomCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "hndxmgtv":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleHunanTelecomMgtvCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "tvmao":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleTvmaoCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "tpl":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.HandleTplCron(0, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "sync":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := service.HandleEpgProgramSyncCron(1, 2); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "genxml":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := service.HandleEpgXmltv1dayGenCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "genxml7":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := service.HandleEpgXmltv7dayGenCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "genxml9":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := service.HandleEpgXmltv9dayGenCron(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "delete7day":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := service.DeleteBefore7dayEpgProgram(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "delete30sday":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := service.DeleteBefore30dayEpgProgram(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	case "syncgitee":
		go func() {
			defer func() {
				if r := recover(); r != nil {
					log.Printf("Cron job failed: %v", r)
				}
			}()

			if err := client.SyncXmlGzToGitee(); err != nil {
				log.Printf("Cron job failed: %v", err)
			} else {
				log.Println("Cron job completed successfully")
			}
		}()
	}

	// 返回响应
	rsp.Success(c, "请求成功", nil)
}
